﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for PacticeHandler
    /// </summary>
    public class eng_PracticeHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Practice     
            string add_practice = context.Request["add_practice"];
            if (!string.IsNullOrEmpty(add_practice))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_practice>(add_practice);
               context.Response.Write("Success Full");
            }
            string view_practice = context.Request["view_practice"];
            if (!string.IsNullOrEmpty(view_practice))
            {
                string Id = context.Request["Id"];
                RenderSinglePractice(context, Convert.ToInt32(Id));
            }
            string list_practice = context.Request["list_practice"];
            if(!string.IsNullOrEmpty(list_practice)){
                RenderPractice(context);
            }
            string delete_practice = context.Request["delete_practice"];
            if (!string.IsNullOrEmpty(delete_practice)) {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_practice>(delete_practice);
                RenderPractice(context);
            }
            string lockunlock_practice = context.Request["lockunlock_practice"];
            if (!string.IsNullOrEmpty(lockunlock_practice)) {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_practice>(lockunlock_practice, "practice_active");
                RenderPractice(context);
            }
            #endregion

            #region Practice Part
            string add_practice_part = context.Request["add_practice_part"];
            if (!string.IsNullOrEmpty(add_practice_part))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_practices_part>(add_practice_part);
                bussiness.InsertAllInOne<eng_part_excercise>(obj.excercise_array_id, "excercise_id", "part_practice_id", obj.part_practice_id);
               context.Response.Write("Success Full");
            }
            string view_practice_part = context.Request["view_practice_part"];
            if (!string.IsNullOrEmpty(view_practice_part))
            {
               string Id = context.Request["Id"];
               RenderSinglePracticePart(context, Convert.ToInt32(Id));
            }

            string list_practice_part = context.Request["list_practice_part"];
            if (!string.IsNullOrEmpty(list_practice_part))
            {
                RenderPracticePart(context);
            }
            string delete_practice_part = context.Request["delete_practice_part"];
            if (!string.IsNullOrEmpty(delete_practice_part))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.Delete<hn_tinhthanh>(delete_practice_part);
                RenderPracticePart(context);
            }
            string lockunlock_practice_part = context.Request["lockunlock_practice_part"];
            if (!string.IsNullOrEmpty(lockunlock_practice_part))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_hedaotao>(lockunlock_practice_part, "part_practice_active");
                RenderPracticePart(context);
            }
            #endregion
        }
        private void RenderSinglePractice(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_practice>(
               c => c.practice_id.Equals(Id),
               c => new
               {
                   id = c.practice_id,
                   levelid=c.level_lecture_id,
                   typecode = c.type_code,
                   title = c.practice_title,
                   active = c.practice_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderPractice(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_practice>(
                null,
                null,
                null,
                c => c.practice_title,
                c => new
                {
                    id = c.practice_id,
                    levelid = c.level_lecture_id,
                    typecode = c.type_code,
                    title = c.practice_title,
                    active = c.practice_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
            context.Response.Write(json);
        }
        private void RenderSinglePracticePart(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_practices_part>(
               c => c.part_practice_id.Equals(Id),
               c => new
               {
                   id = c.part_practice_id,
                   practiceid = c.practice_id,
                   title = c.part_practice_title,
                   active = c.part_practice_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderPracticePart(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_practices_part>(
                null,
                null,
                null,
                c => c.part_practice_title,
                c => new
                {
                    id = c.part_practice_id,
                    practiceid = c.practice_id,
                    title = c.part_practice_title,
                    active = c.part_practice_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}